package com.example.allplan.Fragment;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.allplan.Activity.BianJiChouQianActivity;
import com.example.allplan.ItemTouchHelper.JiHuaBaoTouchCallback;
import com.example.allplan.JavaBean.YiGeChouQian;
import com.example.allplan.MyApplication;
import com.example.allplan.MyView.MyCoordinatorLayout;
import com.example.allplan.R;
import com.example.allplan.RecyclerAdapter.ChouQianLieBiaoAdapter;
import com.example.allplan.StaticFangfa.BiaoQingZhuanHuan;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;
import java.util.Collections;

public class ChouQianFragment extends BaseFragment {

    View mview;
    FloatingActionButton tianjiachouqianbutton;
    TextView biaotibiaoqing;
    RecyclerView mrecyclerview;
    ChouQianLieBiaoAdapter madapter;
    ArrayList<YiGeChouQian> mlist;
    MyApplication myapp;
    ImageView mwushujutu;
    MyCoordinatorLayout mgen;
    boolean jiazaiguotouch = false;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        mview = inflater.inflate(R.layout.chou_qian_fragment_layout, container, false);
        init();
        return mview;
    }

    private void init() {
        mgen = mview.findViewById(R.id.chou_qian_fragment_gen);
        myapp = MyApplication.getInstance();
        mrecyclerview = mview.findViewById(R.id.chou_qian_lie_biao_recyclerView);
        mwushujutu = mview.findViewById(R.id.chou_qian_wu_shu_ju_xian_shi_de_tu_imageView);
        biaotibiaoqing = mview.findViewById(R.id.chou_qian_biao_ti_lan_xian_shi_biao_qing_TextView);
        biaotibiaoqing.setText(BiaoQingZhuanHuan.biao_qing_int_to_string(10067));
        tianjiachouqianbutton = mview.findViewById(R.id.tian_jia_chou_qian_floatbutton);
        tianjiachouqianbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent min = new Intent(getActivity(), BianJiChouQianActivity.class);
                startActivityForResult(min, 91);
            }
        });
    }
    @Override
    protected void lazyLoadData() {
        mlist = myapp.mSuoYouChouQianSQCaoZuo.readall();
        if (mlist.size() <= 0) {
            mwushujutu.setVisibility(View.VISIBLE);
            return;
        } else {
            Collections.sort(mlist);
            mwushujutu.setVisibility(View.GONE);
            madapter = new ChouQianLieBiaoAdapter(mlist, new ChouQianLieBiaoAdapter.onItemClickListener() {
                @Override
                public void onClick(int pos, Context mcon) {
                    Intent min = new Intent(getActivity(), BianJiChouQianActivity.class);
                    min.putExtra("wen_jian_ming", mlist.get(pos).getWen_jian_ming());
                    min.putExtra("biao_ti", mlist.get(pos).getMing_cheng());
                    startActivityForResult(min, 111);
                }
            }, getActivity());
            mrecyclerview.setAdapter(madapter);
            mrecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
        }
        if (!jiazaiguotouch) {
            JiHuaBaoTouchCallback mth = new JiHuaBaoTouchCallback(new JiHuaBaoTouchCallback.itemtouchhelpercallback() {
                @Override
                public void onitemmove(int frompos, int topos) {
                    int temp = mlist.get(frompos).getPai_xu_you_biao();
                    mlist.get(frompos).setPai_xu_you_biao(mlist.get(topos).getPai_xu_you_biao());
                    mlist.get(topos).setPai_xu_you_biao(temp);
                    Collections.swap(mlist, frompos, topos);
                    madapter.notifyItemMoved(frompos, topos);
                    myapp.mSuoYouChouQianSQCaoZuo.updateoneonlypaixu(mlist.get(frompos));
                    myapp.mSuoYouChouQianSQCaoZuo.updateoneonlypaixu(mlist.get(topos));
                }

                @Override
                public void onitemswipe(int direction) {
                    chushihuaalertdialog(direction);


                }
            });
            ItemTouchHelper mhelper = new ItemTouchHelper(mth);
            mhelper.attachToRecyclerView(mrecyclerview);
            jiazaiguotouch = true;
        }

    }

    void shanchu(int direction) {
        myapp.mSuoYouChouQianSQCaoZuo.deleteone(mlist.get(direction));
        getActivity().deleteDatabase(mlist.get(direction).getWen_jian_ming());
        myapp.putSH_Int(myapp.CHOU_QIAN_SHU_SHARE_NAME, --myapp.chou_qian_shu);
        mlist.remove(direction);
        for (int i = direction; i < mlist.size(); i++) {
            mlist.get(i).setPai_xu_you_biao(mlist.get(i).getPai_xu_you_biao() - 1);
            myapp.mSuoYouChouQianSQCaoZuo.updateoneonlypaixu(mlist.get(i));
        }
        madapter.notifyItemRemoved(direction);
        //Log.d("kaishi1",System.currentTimeMillis()+"");
        mgen.setCantclick(true);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(450);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        //madapter.notifyDataSetChanged();
                        mgen.setCantclick(false);
                        //Log.d("kaishi2",System.currentTimeMillis()+"");
                    }
                });
            }
        }).start();
        if (mlist.size() <= 0) {
            mwushujutu.setVisibility(View.VISIBLE);
        }

    }

    void chushihuaalertdialog(final int pos) {
        AlertDialog dialog = new AlertDialog.Builder(getActivity()).setTitle("删除")
                .setMessage("是否删除？")
                .setCancelable(false)
                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        shanchu(pos);
                    }
                })
                .setNegativeButton("否", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        madapter.notifyDataSetChanged();
                    }
                })
                .create();
        dialog.show();
        dialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.lan_se));
        dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.lan_se));


    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (requestCode == 91 && resultCode == 92) {
            lazyLoadData();
        } else if (requestCode == 111 && resultCode == 112) {
            lazyLoadData();
        } else if (requestCode == 91 && resultCode == 112) {
            lazyLoadData();
        }
    }
}
